package com.silence.gankio.receiver

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.silence.gankio.callback.ScanBlueCallBack
import com.xuexiang.rxutil2.logs.RxLog

class BluetoothReceiver(private val callback: ScanBlueCallBack) : BroadcastReceiver() {

    private val TAG = BluetoothReceiver::class.java.simpleName

    //此处为你要连接的蓝牙设备的初始密钥，一般为1234或0000
    private val pin = 1234

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        RxLog.eTag(TAG, "action: $action")

        when (action) {
            //开始搜索
            BluetoothAdapter.ACTION_DISCOVERY_STARTED -> {
                callback.onScanStarted()
            }
            //搜索结束
            BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {
                callback.onScanFinished()
            }
            //发现设备
            BluetoothDevice.ACTION_FOUND -> {
                val btDevice: BluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                callback.onScanning(btDevice)
            }

            BluetoothAdapter.ACTION_STATE_CHANGED -> {
                //<editor-fold>
                when (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)) {
                    BluetoothAdapter.STATE_TURNING_ON -> {
                        RxLog.i("BluetoothAdapter is turning on.")
                        callback.onBluetoothStatus(BluetoothAdapter.STATE_TURNING_ON)
                    }
                    BluetoothAdapter.STATE_ON -> {
                        RxLog.i("BluetoothAdapter is on.")
                        callback.onBluetoothStatus(BluetoothAdapter.STATE_ON)
                    }
                    BluetoothAdapter.STATE_TURNING_OFF -> {
                        callback.onBluetoothStatus(BluetoothAdapter.STATE_TURNING_OFF)
                        RxLog.i("BluetoothAdapter is turning off.")
                    }
                    BluetoothAdapter.STATE_OFF -> {
                        callback.onBluetoothStatus(BluetoothAdapter.STATE_OFF)
                        RxLog.i("BluetoothAdapter is off.")
                    }
                    else -> {
                    }
                }
            }

            BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {
                //<editor-fold>
                val bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE)
                val device: BluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                when (bondState) {
                    BluetoothDevice.BOND_BONDED -> {  //配对成功
                        callback.onBluetoothBondStatus(BluetoothDevice.BOND_BONDED, device)
                        RxLog.i("Device: ${device.name} bonded.")
                    }
                    BluetoothDevice.BOND_BONDING -> {
                        callback.onBluetoothBondStatus(BluetoothDevice.BOND_BONDING, null)
                        RxLog.i("Device: ${device.name} bonding.")
                    }
                    BluetoothDevice.BOND_NONE -> {
                        callback.onBluetoothBondStatus(BluetoothDevice.BOND_NONE, null)
                        RxLog.i("Device: ${device.name} not bonded.")
                        //不知道是蓝牙耳机的关系还是什么原因，经常配对不成功
                        //配对不成功的话，重新尝试配对
                    }
                    else -> {
                    }
                }
            }

            else -> {
            }
        }

    }
}